using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DesktopWidgetApp.Server.Models;
using Microsoft.EntityFrameworkCore;

namespace DesktopWidgetApp.Server.Services
{
    /// <summary>
    /// 状态服务实现
    /// </summary>
    public class StatusService : IStatusService
    {
        private readonly AppDbContext _dbContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">数据库上下文</param>
        public StatusService(AppDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 获取用户的最新状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户的最新状态</returns>
        public async Task<ServerStatus> GetLatestStatusByUserIdAsync(Guid userId)
        {
            return await _dbContext.ServerStatuses
                .Where(s => s.UserId == userId && s.EndTime == null)
                .OrderByDescending(s => s.StartTime)
                .FirstOrDefaultAsync();
        }

        /// <summary>
        /// 添加或更新状态
        /// </summary>
        /// <param name="status">状态对象</param>
        /// <returns>添加或更新后的状态</returns>
        public async Task<ServerStatus> AddOrUpdateStatusAsync(ServerStatus status)
        {
            if (status.Id == Guid.Empty)
            {
                // 添加新状态
                status.Id = Guid.NewGuid();
                status.LastUpdated = DateTime.Now;
                _dbContext.ServerStatuses.Add(status);
            }
            else
            {
                // 更新现有状态
                var existingStatus = await _dbContext.ServerStatuses.FindAsync(status.Id);
                if (existingStatus != null)
                {
                    existingStatus.StatusType = status.StatusType;
                    existingStatus.CustomText = status.CustomText;
                    existingStatus.Color = status.Color;
                    existingStatus.IsDoNotDisturb = status.IsDoNotDisturb;
                    existingStatus.EndTime = status.EndTime;
                    existingStatus.LastUpdated = DateTime.Now;
                }
                else
                {
                    throw new ArgumentException("状态不存在");
                }
            }

            await _dbContext.SaveChangesAsync();
            return status;
        }

        /// <summary>
        /// 获取用户的状态历史
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>状态历史列表</returns>
        public async Task<IEnumerable<ServerStatus>> GetStatusHistoryByUserIdAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _dbContext.ServerStatuses
                .Where(s => s.UserId == userId && s.StartTime >= startDate && s.StartTime <= endDate)
                .OrderByDescending(s => s.StartTime)
                .ToListAsync();
        }
    }
}